package com.lyh.common.config;

import com.lyh.common.utils.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author ljx
 **/
@Configuration
@EnableConfigurationProperties(RedisProperties.class)
public class RedissonConfiguration implements InitializingBean {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Override
    public void afterPropertiesSet() {

        System.out.println("*****************已启动 redis锁！~");
    }


   @Bean
   public RedissonClient getRedisson(RedisProperties properties) {
       Config config = new Config();
       SingleServerConfig singleServerConfig = config.useSingleServer();
       singleServerConfig.setAddress("redis://" + properties.getHost() + ":" + properties.getPort());
       if(StringUtils.isNotEmpty(properties.getPassword())){
           singleServerConfig.setPassword(properties.getPassword());
       }
       return Redisson.create(config);
   }
}
